<?php
	if($_REQUEST['item'] > 0)
	{ 
		$item['id'] = $_REQUEST['item']; 
		$item = getSiteTable($item['id']);
		$cores = getAllCores();
		$siteCores = getSiteCores();
	}
	if($_REQUEST['clientedit'] > 0)
	{ 
		$item['id'] = $_REQUEST['clientedit']; 
		$item = getClient($item['id']);
	}
	switch ($_POST['action'])
	{
		case 'client_list':
			if($_POST['new_client'] != '')
			{	
				addClient($_POST['new_client']);
			}
			if($_POST['client_id'])
			{
				$i=0;
				foreach($_POST['client_id'] as $id)
				{
					updateClient($_POST['client_id'][$i], $_POST['client_name'][$i],  $_POST['client_contact'][$i], $_POST['client_email'][$i], $_POST['client_msn'][$i]);
					$i++;
				}
			}
			break;
		case 'client_edit':
			if($item['id'] > 0)
			{
				updateClient($item['id'], $_POST['name'], $_POST['contact'], $_POST['email'], $_POST['msn'], $_POST['skype'], $_POST['description']);
			}
			break;
		case 'site_list':
			if($_POST['new_title'][0] != '')
			{
				addSite($_POST['new_title'][0], $_GET['client']);
			}
			$i=0;
			if($_POST['site_id'])
			{
				foreach($_POST['site_id'] as $id)
				{
					updateSite($id, $_POST['title'][$i],  $_POST['site_url'][$i], $_POST['license'][$i], $_POST['language'][$i], $_POST['analytics_id'][$i]);
					$i++;
				}
			}
			if($_POST['activate'] > 0)
			{ setTableActive($_POST['activate'], 'sites', 1); }
			if($_POST['deactivate'] > 0)
			{ setTableActive($_POST['deactivate'], 'sites', 0); }
			if($_POST['delete_module'] > 0)
			{ deleteModule($_POST['delete_module']); } 
			
			if($_POST['delete_site'] > 0)
			{ $result = mysql_query('DELETE FROM '.DB_PERFIX.'sites WHERE id='.$_POST['delete_site'].' LIMIT 1;'); }
			break;
		case 'editor':
			if($item['id'] > 0)
			{   
				updateSite($item['id'], $_POST['title'],  $_POST['site_url'], $_POST['license'], $_POST['language']);
				$i=0;
				if($_POST['site_cores'])
				{
					foreach($_POST['site_cores'] as $id)
					{
						if(!($siteCores[$item['id']][$id]))
						{
							if(isset($siteCores[$item['id']][$id]))
							{ setSiteCoreActive($item['id'], $id, 1); }
							else
							{ addSiteCore($item['id'], $id); }
						}
						$i++;
					}
					if(count($siteCores) > 0)
					{
						//foreach(array_keys($row) as $key)
						foreach(array_keys($siteCores[$item['id']]) as $sc)
						{
							$a=0;
							foreach($_POST['site_cores'] as $id)
							{
								if($sc == $id)
								{ $a=1; }
							}
							if($a == 0)
							{ setSiteCoreActive($item['id'], $sc, 0); }
						}
					}
				}
				$cores = getAllCores();
				$siteCores = getSiteCores();
			}
	}
	if($_REQUEST['item'] > 0)
	{ 
		$item['id'] = $_REQUEST['item']; 
		$item = getSiteTable($item['id']);  
	}
	if($_REQUEST['clientedit'] > 0)
	{ 
		$item['id'] = $_REQUEST['clientedit']; 
		$item = getClient($item['id']);
	}
	$clients = getClients();
?>